
#include "stdafx.h"
#include "ttoolbutton.h"

TToolButton::TToolButton(QWidget *parent) 
    : _base(parent)
{
    Init();
}

TToolButton::TToolButton(const QString &text, QWidget *parent)
    : _base(text, parent)
{
    Init();
}

TToolButton::~TToolButton()
{

}

void TToolButton::Init()
{
    m_pressedImage = QImage(":res/images/tool_button_pressed.png");
    m_hoverImage = QImage(":res/images/tool_button_hover.png");
}

void TToolButton::SetBgImage(QString filePath)
{
    QString strStyle = "QPushButton{border-image: url(%1); }";
    strStyle = strStyle.arg(filePath);
    setStyleSheet(strStyle);
}

void TToolButton::paintEvent(QPaintEvent *e)
{
    QPainter p(this);
    if(rect().contains(mapFromGlobal(QCursor::pos())))
    {
        QPen pen(1);
        if(qApp->mouseButtons() == Qt::LeftButton)
        {
            p.drawImage(rect(), m_pressedImage);
        }
        else
        {
            p.drawImage(rect(), m_hoverImage);
        }
        pen.setColor(QColor(229, 195, 101, 200));

        p.setPen(pen);
        p.drawRect(rect().adjusted(0, 0, -1, -1));
    }
    _base::paintEvent(e);
}

void TToolButton::keyPressEvent(QKeyEvent *e)
{
    if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter)
    {
        emit clicked(true);
    }
    _base::keyPressEvent(e);
}
